// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round

char buf[VW_MAX_MESSAGE_LEN];
int i;

void setup()
{
    delay(1000);
    Serial.begin(9600);	  // Debugging only
    Serial.println("INSERT COMMAND ('#' end; '-' cancel)");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec
    
    i = 0;
}

void loop()
{
    if ( Serial.available()) // Check to see if at least one character is available
    {
      char ch = Serial.read();
      buf[i++] = ch;
      
      if(i>=VW_MAX_MESSAGE_LEN)
      {
        Serial.println("COMMAND TOO LONG");
        ch = '-';
      }
      
      if(ch=='#') //SEND COMMAND
      {
        buf[i]='\0';
        i = 0;
        digitalWrite(13, true); // Flash a light to show transmitting
        delay(150);
        vw_send((uint8_t *)buf, strlen(buf));
        vw_wait_tx(); // Wait until the whole message is gone
        Serial.print("SEND: ");
        Serial.println(buf);
        digitalWrite(13, false);
        delay(1000);                
      }
      if(ch=='-') //CANCEL COMMAND
      {
        buf[i]='\0';
        i = 0;
        Serial.print("CANCEL: ");
        Serial.println(buf);
      }
    }  

}
